<template>
  <view v-if="show" class="fu-dialog__wrapper">
    <view class="fu-dialog__mask" @click="onClickMask"></view>
    <view 
      class="fu-dialog"
      :class="[
        `fu-dialog--${type}`,
        { 'fu-dialog--no-title': !title }
      ]"
    >
      <!-- 标题 -->
      <view v-if="title" class="fu-dialog__header">
        <text class="fu-dialog__title">{{ title }}</text>
      </view>
      
      <!-- 内容 -->
      <view class="fu-dialog__content">
        <text v-if="message" class="fu-dialog__message">{{ message }}</text>
        <slot></slot>
      </view>
      
      <!-- 按钮组 -->
      <view 
        class="fu-dialog__footer"
        :class="{ 'fu-dialog__footer--column': buttonLayout === 'column' }"
      >
        <!-- 取消按钮 -->
        <view
          v-if="showCancelButton"
          class="fu-dialog__button fu-dialog__button--cancel"
          :style="{ color: cancelButtonColor }"
          @click="onCancel"
        >{{ cancelButtonText }}</view>
        
        <!-- 确认按钮 -->
        <view
          class="fu-dialog__button fu-dialog__button--confirm"
          :class="[`fu-dialog__button--${type}`]"
          @click="onConfirm"
        >{{ confirmButtonText }}</view>
      </view>
    </view>
  </view>
</template>

<script>
/**
 * fu-dialog 对话框组件
 * @description 模态对话框，用于消息提示、确认消息等交互
 * @property {Boolean} show - 是否显示
 * @property {String} title - 标题
 * @property {String} message - 文本内容
 * @property {String} type - 类型，可选值为 default primary success warning error
 * @property {Boolean} showCancelButton - 是否显示取消按钮
 * @property {String} cancelButtonText - 取消按钮文字
 * @property {String} cancelButtonColor - 取消按钮颜色
 * @property {String} confirmButtonText - 确认按钮文字
 * @property {String} buttonLayout - 按钮布局方式，可选值为 row column
 * @property {Boolean} closeOnClickMask - 是否在点击遮罩层后关闭
 * @event {Function} confirm - 点击确认按钮时触发
 * @event {Function} cancel - 点击取消按钮时触发
 */
export default {
  name: 'fu-dialog',
  props: {
    show: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ''
    },
    message: {
      type: String,
      default: ''
    },
    type: {
      type: String,
      default: 'default',
      validator: value => ['default', 'primary', 'success', 'warning', 'error'].includes(value)
    },
    showCancelButton: {
      type: Boolean,
      default: false
    },
    cancelButtonText: {
      type: String,
      default: '取消'
    },
    cancelButtonColor: {
      type: String,
      default: 'var(--fu-text-color)'
    },
    confirmButtonText: {
      type: String,
      default: '确定'
    },
    buttonLayout: {
      type: String,
      default: 'row',
      validator: value => ['row', 'column'].includes(value)
    },
    closeOnClickMask: {
      type: Boolean,
      default: false
    }
  },
  emits: ['update:show', 'confirm', 'cancel'],
  methods: {
    onClickMask() {
      if (this.closeOnClickMask) {
        this.onCancel()
      }
    },
    onConfirm() {
      this.$emit('update:show', false)
      this.$emit('confirm')
    },
    onCancel() {
      this.$emit('update:show', false)
      this.$emit('cancel')
    }
  }
}
</script>

<style lang="scss" scoped>
.fu-dialog {
  position: relative;
  width: 600rpx;
  background: #fff;
  border-radius: 24rpx;
  overflow: hidden;
  animation: fu-zoom-in 0.3s;
  
  &__wrapper {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 9999;
    width: 100%;
    height: 100%;
    overflow: hidden;
  }
  
  &__mask {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    animation: fu-fade-in 0.3s;
  }
  
  &__header {
    padding: 48rpx 48rpx 0;
    text-align: center;
  }
  
  &__title {
    font-size: 36rpx;
    font-weight: 600;
    color: var(--fu-text-color);
    line-height: 1.4;
  }
  
  &__content {
    padding: 32rpx 48rpx;
    max-height: 60vh;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    
    .fu-dialog.fu-dialog--no-title & {
      padding-top: 48rpx;
    }
  }
  
  &__message {
    font-size: 30rpx;
    color: var(--fu-text-color);
    line-height: 1.6;
    text-align: center;
  }
  
  &__footer {
    display: flex;
    align-items: center;
    border-top: 2rpx solid var(--fu-border-color);
    
    &--column {
      flex-direction: column;
      
      .fu-dialog__button {
        width: 100%;
        position: relative;
        flex: none;
        
        &::after {
          content: '';
          position: absolute;
          left: 0;
          right: 0;
          bottom: 0;
          height: 2rpx;
          background-color: var(--fu-border-color);
        }
        
        &:last-child {
          &::after {
            display: none;
          }
        }
      }
    }
  }
  
  &__button {
    flex: 1;
    height: 100rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 32rpx;
    cursor: pointer;
    
    &:active {
      opacity: 0.7;
    }
    
    &--cancel {
      position: relative;
      
      &::after {
        content: '';
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        width: 2rpx;
        background-color: var(--fu-border-color);
      }
      
      .fu-dialog__footer--column & {
        &::after {
          display: none;
        }
      }
    }
    
    &--confirm {
      font-weight: 600;
    }
    
    &--primary {
      color: var(--fu-primary-color);
    }
    
    &--success {
      color: var(--fu-success-color);
    }
    
    &--warning {
      color: var(--fu-warning-color);
    }
    
    &--error {
      color: var(--fu-danger-color);
    }
    
    &--default {
      color: var(--fu-primary-color);
    }
  }
}

@keyframes fu-fade-in {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes fu-zoom-in {
  from {
    transform: scale(0.8);
    opacity: 0;
  }
  to {
    transform: scale(1);
    opacity: 1;
  }
}
</style> 